package com.example.mediademo.decode;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.example.mediademo.Configs;
import com.example.mediademo.decode.extractor.VideoExtractor;
import com.example.mediademo.decode.interfaces.IDecoderProgress;
import com.example.mediademo.decode.interfaces.IExtractor;

import java.nio.ByteBuffer;

/**
 * 视频解码器
 * @author baj
 */
public class VideoDecoder extends BaseDecoder{

    private SurfaceView surfaceView;
    private Surface surface;

    public VideoDecoder(String filePath, SurfaceView surfaceView, Surface surface) {
        super(filePath);
        this.surfaceView = surfaceView;
        this.surface = surface;
    }

    /**
     * 初始化时检查
     * @return
     */
    @Override
    protected boolean check() {
        if (surfaceView == null && surface == null) {
            Log.w(Configs.TAG,"SurfaceView和Surface都为空，至少需要一个不为空");
            mStateListener.decodeError(this,"Surface为空");
            return false;
        }
        return true;
    }

    /**
     * 初始化数据提取器
     * @return
     */
    @Override
    protected IExtractor initExtractor() {
        return new VideoExtractor(mFilePath);
    }

    /**
     * 配置独特参数
     * @param format MediaFormat
     */
    @Override
    protected void initSpecParams(MediaFormat format) {

    }

    /**
     * 初始化渲染器
     * @return 是否成功
     */
    @Override
    protected boolean initRender() {
        return true;
    }


    @Override
    protected boolean configCodec(MediaCodec mCodec, MediaFormat format) {
        if (surface != null) {
            mCodec.configure(format,surface,null,0);
            notifyDecode();
        } else if (surfaceView != null && surfaceView.getHolder().getSurface() != null) {
            surface = surfaceView.getHolder().getSurface();
            configCodec(mCodec,format);
        } else {
            surfaceView.getHolder().addCallback(new SurfaceHolder.Callback2() {
                @Override
                public void surfaceRedrawNeeded(@NonNull SurfaceHolder surfaceHolder) {
                }

                @Override
                public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                    surface = surfaceHolder.getSurface();
                    configCodec(mCodec,format);
                }

                @Override
                public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                }

                @Override
                public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
                }
            });
            return false;
        }
        return true;
    }

    @Override
    protected void render(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo) {

    }

    @Override
    protected void doneDecode() {

    }

    @Override
    public void setSizeListener(IDecoderProgress listener) {

    }
}
